from rest_framework.generics import ListAPIView, ListCreateAPIView
from rest_framework.permissions import IsAdminUser

from apps.meiduo_admin.serializers.users import UserSerializer
from apps.meiduo_admin.utils import UserPageNum
from apps.users.models import User


# 用户的查询获取和增加
class UserView(ListCreateAPIView):
    # 指定查询集
    queryset = User.objects.filter(is_staff=False)
    # 指定序列化器
    serializer_class = UserSerializer
    # 指定分页器
    pagination_class = UserPageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    # 重写get_queryset方法，根据前端是否传递keyword值返回不同查询结果
    def get_queryset(self):
        # 获取前端传递的keyword值
        keyword = self.request.query_params.get('keyword')
        # 如果keyword是空字符，则说明要获取所有用户数据
        if keyword is None or keyword == '':
            return User.objects.filter(is_staff=False)
        else:
            return User.objects.filter(username__contains=keyword)